package com.im.app.processor;

import com.im.app.model.ChatLogs;
import com.im.app.model.Conversations;
import com.im.app.model.User;
import com.im.app.service.*;
import com.im.app.utils.TokenUtils;
import org.jim.core.ImChannelContext;
import org.jim.core.cache.redis.JedisTemplate;
import org.jim.core.packets.*;
import org.jim.server.processor.HttpCmdProcessor;
import org.jim.server.protocol.AbstractProtocolCmdProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component("httpServiceProcessor")
public class HttpServiceProcessor extends AbstractProtocolCmdProcessor implements HttpCmdProcessor {

    private Logger logger = LoggerFactory.getLogger(HttpServiceProcessor.class);

    private static Map<String,User> cacheUser = new HashMap<>();

    public static HttpServiceProcessor ls;

    @Autowired
    private IUserService userService;

    @Autowired
    private ICodeService codeService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private IPersonhostService personhostService;

    @Autowired
    private VisitorAvatarService visitorAvatarService;

    @Autowired
    private ConversationsService conversationsService;

    @Autowired
    private ChatLogsService chatLogsService;

    @PostConstruct
    public void init() {
        ls = this;
        ls.userService = this.userService;
        ls.codeService = this.codeService;
        ls.request=this.request;
        ls.personhostService=this.personhostService;
        ls.visitorAvatarService=this.visitorAvatarService;
        ls.conversationsService=this.conversationsService;
        ls.chatLogsService=this.chatLogsService;
    }


    /**
     * 查询用户信息
     * */
    @Override
    public HttpRespBody getUserInfo(HttpReqBody httpReqBody, ImChannelContext imChannelContext) {
        HttpRespBody httpRespBody = HttpRespBody.success();
        User user=ls.userService.getUserByUserId(httpReqBody.getUserId());
        if(null!=user){
            httpRespBody.setData(user);
        }else{
            httpRespBody = HttpRespBody.failed();
        }
        return httpRespBody;
    }


    /**
     * 客服登录根据客服id、时间查询会话列表
     * */
    @Override
    public HttpRespBody getConversationsList(HttpReqBody httpReqBody, ImChannelContext imChannelContext) {
        HttpRespBody httpRespBody = HttpRespBody.success();
        List<Conversations> conversationsList = ls.conversationsService.getConversationsList(httpReqBody.getOpUserId());
        httpRespBody.setData(conversationsList);
        return httpRespBody;
    }

    /**
     * 撤回消息记录
     * */
    @Override
    public HttpRespBody cancelChatLog(HttpReqBody httpReqBody, ImChannelContext imChannelContext) {
        HttpRespBody httpRespBody = HttpRespBody.success();
        int result = ls.chatLogsService.cancelChatLog(httpReqBody.getOpUserId(),httpReqBody.getMsgId());
        if(result>0){
            httpRespBody.setData("撤回成功");
        }else{
            httpRespBody = HttpRespBody.failed("撤回失败");
        }
        return httpRespBody;
    }

    /**
     * 清除未读数
     * */
    @Override
    public HttpRespBody clearUnreadCount(HttpReqBody httpReqBody, ImChannelContext imChannelContext) {
        HttpRespBody httpRespBody = HttpRespBody.success();
        int result = ls.conversationsService.clearUnreadCount(httpReqBody.getConversationId());
        ls.chatLogsService.updateIsRead(httpReqBody.getOpUserId(),httpReqBody.getUserId());
        if(result>0){
            httpRespBody.setData("清除成功");
        }else{
            httpRespBody = HttpRespBody.failed("清除失败");
        }
        return httpRespBody;
    }
    /**
     * 会话置顶
     * */
    @Override
    public HttpRespBody updateIsPinned(HttpReqBody httpReqBody, ImChannelContext imChannelContext) {
        HttpRespBody httpRespBody = HttpRespBody.success();
        int result = ls.conversationsService.updateIsPinned(httpReqBody.getIsPinned(),httpReqBody.getConversationId());
        if(result>0){
            httpRespBody.setData("置顶成功");
        }else{
            httpRespBody = HttpRespBody.failed("置顶失败");
        }
        return httpRespBody;
    }
    /**
     * 清除消息记录
     * */
    @Override
    public HttpRespBody clearChatLog(HttpReqBody httpReqBody, ImChannelContext imChannelContext) {
        HttpRespBody httpRespBody = HttpRespBody.success();
        int result = ls.chatLogsService.clearChatLog(httpReqBody.getOpUserId(),httpReqBody.getUserId());
        if(result>0){
            httpRespBody.setData("清除成功");
        }else{
            httpRespBody = HttpRespBody.failed("清除失败");
        }
        return httpRespBody;
    }
    /**
     * 清除会话
     * */
    @Override
    public HttpRespBody clearConversations(HttpReqBody httpReqBody, ImChannelContext imChannelContext) {
        HttpRespBody httpRespBody = HttpRespBody.success();
        int result = ls.conversationsService.clearConversations(httpReqBody.getConversationId());
        if(result>0){
            httpRespBody.setData("清除成功");
        }else{
            httpRespBody = HttpRespBody.failed();
        }
        return httpRespBody;
    }
    /**
     * 查询消息记录
     * */
    @Override
    public HttpRespBody getChatLogsList(HttpReqBody httpReqBody, ImChannelContext imChannelContext) {
        HttpRespBody httpRespBody = HttpRespBody.success();
        List<ChatLogs> result = null ;
         if(httpReqBody.getGroupId()!=null){
//              result = ls.chatLogsService.getChatLogsList(httpReqBody.getOpUserId(),httpReqBody.getGroupId());
         }else {
              result = ls.chatLogsService.getChatLogsList(httpReqBody.getOpUserId(),httpReqBody.getUserId(),httpReqBody.getCreateTime());
         }
        httpRespBody.setData(result);
        return httpRespBody;
    }






}
